/*
 * Albareth - an action roleplaying game.
 * Copyright (C) 2008 Jens Anuth
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 *  02110-1301, USA.
 * 
 * $Id: CharCreationFrame.java 49 2008-05-17 02:35:15Z jens464 $
*/
package albareth.frontend.slick.ui;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;

import albareth.core.Game;
import albareth.core.action.SpellBlink;
import albareth.core.action.SpellFireball;
import albareth.core.action.SpellFrostnova;
import albareth.core.action.SpellHeal;
import albareth.core.action.SpellMagicMissile;
import albareth.core.entity.EquipableItem;
import albareth.core.entity.ItemFactory;
import albareth.core.entity.Mobile;
import albareth.core.entity.PlayerCharacter;
import albareth.frontend.Resources;
import albareth.frontend.Tile;
import albareth.frontend.TileCollection;
import albareth.frontend.slick.SlickFrontend;
import albareth.frontend.slick.SlickResources;
import frosch.util.Log;
import frosch.util.glider.Easing;

public class CharCreationFrame extends GlidingScroll implements
    MenuListener, UIButtonListener
{
    private static Color _labelColor = new Color(120, 18, 0);
    private static Color _inputColor = new Color(0, 167, 0);
    
    private BasicMenuFrame _avatarMenu;
    private BasicMenuFrame _pathMenu;
    private UITextField _nameInputField;

    public CharCreationFrame(int start, SlickFrontend frontend)
    {
        super(start, frontend);
        
        UIButton button = new UIButton(510, 237, 89, 27);
        button.setForegroundColor(Color.yellow);
        button.setText("OK");
        button.setPadding(25, 4);
        button.setMouseUpImage(SlickResources.BUTTON);
        button.setMouseDownImage(SlickResources.BUTTON_PRESSED);
        button.setMouseOverImage(SlickResources.BUTTON_HOVER);
        button.addButtonListener(this);
        addChild(button);
        
        _nameInputField = new UITextField(380, 63, 215, 20);
        _nameInputField.setForegroundColor(_inputColor);
        _nameInputField.setMaxTextLength(14);
        addChild(_nameInputField);
        UIManager.requestKeyboardFocus(_nameInputField);
        
        _pathMenu = new BasicMenuFrame(
                new ListMenuView(3, 1, 20)
        );
        _pathMenu.setForegroundColor(_inputColor);
        _pathMenu.setBackgroundColor(_labelColor);
        _pathMenu.addItem("MAGE", 1);
        _pathMenu.addItem("WARRIOR", 2);
        _pathMenu.addItem("ROGUE", 3);
        _pathMenu.getItem(1).setDisabled(true);
        _pathMenu.getItem(2).setDisabled(true);
        _pathMenu.addMenuListener(this);
        _pathMenu.selectOnMousePress(true);
        _pathMenu.setLocation(380, 128);
        _pathMenu.setSize(210, 100);
        addChild(_pathMenu);
        
        _avatarMenu = new BasicMenuFrame(
                new IconGridMenuView(32, 32, 6, 6)
        );
        _avatarMenu.setBackgroundColor(_labelColor);
        _avatarMenu.addMenuListener(this);
        _avatarMenu.setSize(210, 210);
        _avatarMenu.selectOnMousePress(true);
        _avatarMenu.setLocation(33, 58);
        _avatarMenu.setPadding(8, 8);
        
        //_avatarMenu.addItem(createAvatarItem(TileCollection.NAKED_MAN));      
        _avatarMenu.addItem(createAvatarItem(TileCollection.MAN));            
        _avatarMenu.addItem(createAvatarItem(TileCollection.WOMAN));          
        _avatarMenu.addItem(createAvatarItem(TileCollection.MAGE_BLUE));      
        _avatarMenu.addItem(createAvatarItem(TileCollection.MAGE_GRAY));      
        _avatarMenu.addItem(createAvatarItem(TileCollection.BEGGAR));         
        _avatarMenu.addItem(createAvatarItem(TileCollection.CAVEMAN));        
        _avatarMenu.addItem(createAvatarItem(TileCollection.RANGER));         
        _avatarMenu.addItem(createAvatarItem(TileCollection.RANGER_BOW));     
        _avatarMenu.addItem(createAvatarItem(TileCollection.KNIGHT));         
        _avatarMenu.addItem(createAvatarItem(TileCollection.KNIGHT_SPEAR));   
        _avatarMenu.addItem(createAvatarItem(TileCollection.NINJA));          
        _avatarMenu.addItem(createAvatarItem(TileCollection.NINJA_SWORD));    
        _avatarMenu.addItem(createAvatarItem(TileCollection.MAN_RED));        
        _avatarMenu.addItem(createAvatarItem(TileCollection.WOMAN_GREEN));    
        _avatarMenu.addItem(createAvatarItem(TileCollection.MAGE_BLUE_STAFF));
        _avatarMenu.addItem(createAvatarItem(TileCollection.MAGE_GRAY_STAFF));
        _avatarMenu.addItem(createAvatarItem(TileCollection.MINSTREL));       
        _avatarMenu.addItem(createAvatarItem(TileCollection.CAVEMAN_CLUB));   
        _avatarMenu.addItem(createAvatarItem(TileCollection.RANGER_SPEAR));   
        _avatarMenu.addItem(createAvatarItem(TileCollection.RANGER_SWORD));   
        _avatarMenu.addItem(createAvatarItem(TileCollection.KNIGHT_SWORD));   
        _avatarMenu.addItem(createAvatarItem(TileCollection.KNIGHT_HAMMER));  
        _avatarMenu.addItem(createAvatarItem(TileCollection.NINJA_BOW));      
        _avatarMenu.addItem(createAvatarItem(TileCollection.NINJA_SPEAR));
        
        addChild(_avatarMenu);
    }

    private MenuItem createAvatarItem(Tile tile)
    {
        MenuItem item = new MenuItem("Avatar");
        try {
            item.setIconImage(SlickResources.getTileImage(tile));
            item.setReferencedObject(tile);
        } catch (SlickException e) {
            Log.error("Unable to get tile image for menu icon" + e);
        };
        return item;
    }

    public void render(Graphics g, GameContainer container, int screenX,
            int screenY) throws SlickException
    {
        super.render(g, container, screenX, screenY);
        
        SlickResources.CREATE_YOUR_CHAR.draw(screenX + 130, screenY - 9);
        
        SlickResources.AVATAR_CHOOSE_BG.draw(screenX + 33, screenY + 58);
        
        g.setColor(_labelColor);
        g.drawString("AVATAR:", screenX + 43, screenY + 37);
        g.drawString("NAME:", screenX + 270, screenY + 63);
        SlickResources.RULER.draw(screenX + 380, screenY + 78);
        g.drawString("PATH:", screenX + 270, screenY + 128);


    }

    public void menuCloseRequested(BasicMenuFrame menu)
    {
        // TODO Auto-generated method stub
        
    }

    public void menuItemSelected(BasicMenuFrame menu, MenuItem selectedItem)
    {
        // TODO Auto-generated method stub
        
    }

    public void buttonPressed(UIButton button)
    {
        _frontend.playSound(Resources.SOUND_BUTTON_POP);
        
        UIManager.releaseKeyboardFocus(_nameInputField);
        
        String name = _nameInputField.getText();
        if (name.trim().equals("")) {
            name = "ANO NYMOUS";
        }
        
        Tile tile = (Tile)_avatarMenu.getSelectedObject();
        if (tile == null) {
            tile = TileCollection.NAKED_MAN;
        }
        
        
        //create player character:
        
        PlayerCharacter player = new PlayerCharacter(name, tile , 50, 50);

        if (Game.CHEAT_MODE) {
            player.setFlags(Mobile.FLAG_INVUL);
        }
        
        //TODO: selected class.
        
        player.setStr(10);
        player.setDex(10);
        player.setInt(10);
        //player.setMana(1);
        //player.setHitpoints(1);
        //player.setExperience(74);

        //TODO: spell factory
        player.addSpell(new SpellBlink());
        player.addSpell(new SpellMagicMissile());
        player.addSpell(new SpellFrostnova());
        player.addSpell(new SpellFireball());
        player.addSpell(new SpellHeal());
       
        /*
        Spell bone = new SpellShootProjectile("Bone", TileCollection.BONE, null, true, new Range(1,6), 0);
        bone.setCastingDuration(0);
        bone.setCooldownDuration(0);
        player.addSpell(bone);
        */
        

        //give starting items
        ItemFactory factory = ItemFactory.getInstance();
        
        player.addItem(factory.createItem("i_bomb", 15));
        player.addItem(factory.createItem("i_potion_heal", 5));
        player.addItem(factory.createItem("i_potion_mana", 5));
        //player.addItem(factory.createItem("i_scroll_town_portal"));
        player.addItem(factory.createItem("i_apple", 3));
        player.addItem(factory.createItem("i_beer", 2));
        player.tryEquip((EquipableItem)factory.createItem("i_padded_shirt"));
        player.tryEquip((EquipableItem)factory.createItem("i_dagger"));

        _frontend.enterWorld(player);
        
    }

}
